package alg20241202;

import java.util.Scanner;

public class Text {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), sumV = in.nextInt();
        int[] v = new int[n];   //物品体积
        int[] w = new int[n];   //物品价值
        for (int i = 0; i < n; i++) {
            v[i] = in.nextInt();
            w[i] = in.nextInt();
        }
        //1 创建表
        int[][] dp1 = new int[n + 1][sumV + 1];
        int[][] dp2 = new int[n + 1][sumV + 1];
        //2 初始化
        for (int j = 1; j <= sumV; j++) {
            dp2[0][j] = -1;
        }

        //3 填表
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= sumV; j++) {
                dp1[i][j] = dp1[i - 1][j];
                if(j - v[i- 1] >= 0) {
                    dp1[i][j] = Math.max(dp1[i - 1][j], dp1[i - 1][j - v[i - 1]] + w[i - 1]);
                }
            }
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= sumV; j++) {
                dp2[i][j] = dp2[i - 1][j];
                if(j - v[i - 1] >= 0 && dp2[i - 1][j - v[i - 1]] != -1){
                    dp2[i][j] = Math.max(dp2[i - 1][j], dp2[i - 1][j - v[i - 1]] + w[i - 1]);
                }
            }
        }
        //4 返回值
        System.out.println(dp1[n][sumV]);
        System.out.println(dp2[n][sumV]);
    }
}
